package org.halk.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 获取方法执行时间
 *
 * @Author halk
 * @Date 2023/4/28 15:41
 **/
@Aspect
@Component
public class MapperAspect {

    private static final Logger log = LoggerFactory.getLogger(MapperAspect.class);

    @AfterReturning("execution(* org.halk.mapper.*Mapper.*(..))")
    public void logServiceAccess(JoinPoint joinPoint) {
        log.info("Completed: ");
    }


    /**
     * 监控*Mapper包及其子包的所有public方法
     */
    @Pointcut("execution(* org.halk.mapper.*Mapper.*(..))")
    private void pointCutMethod() {
    }

    /**
     * 声明环绕通知
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.nanoTime();
        Object obj = pjp.proceed();
        long end = System.nanoTime();
        log.info("耗时：{}毫秒",
                (end - begin) / 1000000);
        return obj;
    }
}
